<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site_config extends Controller
{
	function Site_config()
	{
		parent::Controller();
		check_is_login();
		$this->load->library('form_validation');
	}
	function index()
	{
		$this->load->config("customer_image_config");
		$this->load->config("customer_site_config");
		$this->load->config("customer_email_config");
		$font_size=array(14,16,18,20,22,24,26,28,30,32,34,36);//水印字体
		$data=array(
			'edit_data' => $this->config->config,
			'font_size' => $font_size
		);
		$this->load->view(TPL_FOLDER."site_config",$data);
	}
	
	function save_record()
	{
		$this->form_validation->set_rules("sys_is_close","网站开关","trim|integer");
		$this->form_validation->set_rules("sys_site_name","网站名称","trim|require");
		$this->form_validation->set_rules("sys_site_title","网站标题","trim|require");
		$this->form_validation->set_rules("sys_site_keyword","META关键字","trim|require");
		$this->form_validation->set_rules("sys_site_description","META描述","trim|require");
		$this->form_validation->set_rules("sys_site_copyright","网站版权","trim|require");
		$this->form_validation->set_rules("sys_cache_time","网页缓存","trim|require|integer");
		$this->form_validation->set_rules("sys_corp","公司名称","trim|require");
		$this->form_validation->set_rules("thumb_width","缩略图宽度","trim|integer");
		$this->form_validation->set_rules("thumb_height","缩略图高度","trim|integer");
		$this->form_validation->set_rules("wm_type","水印类型","trim|require");
		$this->form_validation->set_rules('smtp_email','发信邮箱','required|valid_email');
		$this->form_validation->set_rules('smtp_re_email','通知邮箱','required|valid_email');
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		$sys_site_logo=do_upload(array('up_path'=>UP_IMAGES_PATH,'form_name'=>"sys_site_logo",'suffix'=>UP_IMAGES_EXT));
		$wm_overlay_path=do_upload(array('up_path'=>UP_IMAGES_PATH,'form_name'=>"wm_overlay_path",'suffix'=>UP_IMAGES_EXT));
		if(!$sys_site_logo['status'])
		{
			$tx_msg.=$sys_site_logo['upload_errors'];
		}
		if(!$wm_overlay_path['status'])
		{
			$tx_msg.=$wm_overlay_path['upload_errors'];
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		$this->load->helper('file');
		$this->load->helper('string');
		$string=read_file(APPPATH.'config/customer_site_config'.EXT);
		$config['sys_site_name'] = strip_quotes($this->input->post("sys_site_name"));
		$config['sys_site_title'] = strip_quotes($this->input->post("sys_site_title"));
		if($sys_site_logo['file_path'])
		{
			$config['sys_site_logo'] = $sys_site_logo['file_path'];
		}
		else
		{
			$config['sys_site_logo'] = strip_quotes($this->input->post("pic_path1"));
		}
		$config['sys_site_keyword'] = strip_quotes($this->input->post("sys_site_keyword"));
		$config['sys_site_description'] = strip_quotes($this->input->post("sys_site_description"));
		$config['sys_site_copyright'] = strip_quotes($this->input->post("sys_site_copyright"));
		$config['sys_is_open_im'] = strip_quotes($this->input->post("sys_is_open_im"));
		$config['sys_corp'] = strip_quotes($this->input->post("sys_corp"));
		$config['sys_address'] = strip_quotes($this->input->post("sys_address"));
		$config['sys_tel'] = strip_quotes($this->input->post("sys_tel"));
		$config['sys_fax'] = strip_quotes($this->input->post("sys_fax"));
		$config['sys_mobile'] = strip_quotes($this->input->post("sys_mobile"));
		$config['sys_email'] = strip_quotes($this->input->post("sys_email"));
		$config['sys_cache_time'] = strip_quotes($this->input->post("sys_cache_time"));
		$reg ='/\$config\[\'(.+?)\'\].+?;/is';
		preg_match_all( $reg ,$string , $out  );
		if( $out[1] )
		{
			$old = $new = array();
			foreach( $out[1] as $k => $v )
			{
				if(isset($config[strtolower($v)]))
				{
					$old[] = $out[0][$k];
					$new[] = '$config[\''.strtolower($v).'\'] = "'.$config[strtolower($v)].'";';
				}				
			}
			if( $new )
			{
				$string = str_replace( $old , $new , $string );
				write_file(APPPATH.'config/customer_site_config'.EXT, $string);
			}
		}
		unset($config); unset($out);unset($old); unset($new);
		
		$string=read_file(APPPATH.'config/customer_image_config'.EXT);
		$wm_hor_vrt=explode("|",strip_quotes($this->input->post("wm_hor_vrt")));
		$config['wm_hor_alignment'] = $wm_hor_vrt[0];
		$config['wm_vrt_alignment'] = $wm_hor_vrt[1];
		if($wm_overlay_path['file_path'])
		{
			$config['wm_overlay_path'] = $wm_overlay_path['file_path'];
		}
		else
		{
			$config['wm_overlay_path'] = $this->input->post("pic_path2");
		}
		$config['thumb_width'] = strip_quotes($this->input->post("thumb_width"));
		$config['thumb_height'] = strip_quotes($this->input->post("thumb_height"));
		$config['wm_text'] = strip_quotes($this->input->post("wm_text"));
		$config['wm_type'] = strip_quotes($this->input->post("wm_type"));
		$config['wm_font_size'] = strip_quotes($this->input->post("wm_font_size"));
		$config['wm_font_color'] = strip_quotes($this->input->post("wm_font_color"));
		$reg ='/\$config\[\'(.+?)\'\].+?;/is';
		preg_match_all( $reg ,$string , $out  );
		if( $out[1] )
		{
			//$old = $new = array();
			foreach( $out[1] as $k => $v )
			{
				if(isset($config[strtolower($v)]))
				{
					$old[] = $out[0][$k];
					$new[] = '$config[\''.strtolower($v).'\'] = "'.$config[strtolower($v)].'";';
				}			
			}
			if( $new )
			{
				$string = str_replace( $old , $new , $string );
				write_file(APPPATH.'config/customer_image_config'.EXT, $string);
			}
		}
		unset($config); unset($out);unset($old); unset($new);
		
		$string=read_file(APPPATH.'config/customer_email_config'.EXT);
		$config['smtp_host'] = strip_quotes($this->input->post("smtp_host"));
		$config['smtp_user'] = strip_quotes($this->input->post("smtp_user"));
		$config['smtp_pass'] = strip_quotes($this->input->post("smtp_pass"));
		$config['smtp_email'] = strip_quotes($this->input->post("smtp_email"));
		$config['smtp_re_email'] = strip_quotes($this->input->post("smtp_re_email"));
		$reg ='/\$config\[\'(.+?)\'\].+?;/is';
		preg_match_all( $reg ,$string , $out  );
		if( $out[1] )
		{
			//$old = $new = array();
			foreach( $out[1] as $k => $v )
			{
				if(isset($config[strtolower($v)]))
				{
					$old[] = $out[0][$k];
					$new[] = '$config[\''.strtolower($v).'\'] = "'.$config[strtolower($v)].'";';
				}			
			}
			if( $new )
			{
				$string = str_replace( $old , $new , $string );
				write_file(APPPATH.'config/customer_email_config'.EXT, $string);
			}
		}
		unset($config); unset($out);unset($old); unset($new);
		echo_msg('<li>配置修改成功</li>','','yes');
	}

}
?>